<?php
/**
*
* @author Blackpawn blackpawn666@hotmail.com - http://blackpawnsarchives.net
* @co_author GameCharmer admin@gamecharmer.com - http://gaming.gamecharmer.com
* @package Age of Dragons Mod :: ( Game Console Required )
* @version $Id: land.php 1.1.1 12-26-2009 Blackpawn
* @copyright (c) 2009 Blackpawns Archives
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/

define('GC_MOD', true);
define('IN_PHPBB', true);
define('AOD', true);

$phpbb_root_path =  '../../';
$templates_path = '../../../games/aod/templates/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/mods/game_console/gc_common.' . $phpEx);
include($phpbb_root_path . 'games/aod/common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

// Set custom language path
$user->set_custom_lang_path($phpbb_root_path.'games/aod/language/');

//load our language file
$user->add_lang('common');

// Now add the phpBB language path
$user->set_custom_lang_path($phpbb_root_path . 'language');

// Load the Game Console language file
$user->add_lang('mods/game_console');

if ($user->data['user_id'] == ANONYMOUS)
{
	login_box('', $user->lang['LOGIN']);
}

if ($user->data['is_bot'])
{
	redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
}

// Default Vars
$land_page = TRUE; 	// Highlights the land tab
$error_msgs = null;
$admin = 0;
$gold_total = 0;
$food_total = 0;
$gold_total = 0;
$gold_total = 0;

// Var Requests
$cmd = request_var('cmd','');

// AOD Config
$land_gold = $aod_config['land_gold'];
$penalty = ($aod_config['land_cost_penalty'] / 100);

if($penalty < 0)
{
	$penalty = 0;
}

// Gather some user info
$user_id = $user->data['user_id'];
$account_status = check_account_status($user_id);
if ($account_status == FALSE)
{
	redirect(append_sid("{$phpbb_root_path}/games/index.$phpEx"));
}

// Fetch kingdom data
$king = fetch_kingdom_data($user_id);
$admin_access = $king['admin_access'];
$kingdom_sponsor = $king['kingdom_sponsor'];
$kingdom_level = $king['kingdom_level'];
$kingdom_net = $king['kingdom_net'];
$kingdom_land = $king['kingdom_land'];
$kingdom_farms = $king['kingdom_farms'];
$kingdom_timber_mills = $king['kingdom_timber_mills'];
$kingdom_iron_mines = $king['kingdom_iron_mines'];
$kingdom_gold_mines = $king['kingdom_gold_mines'];
$kingdom_towers	= $king['kingdom_towers'];
$kingdom_granaries = $king['kingdom_granaries'];
$kingdom_woodsheds = $king['kingdom_woodsheds'];
$kingdom_ironpits = $king['kingdom_ironpits'];
$kingdom_vaults = $king['kingdom_vaults'];
$kingdom_food = $king['kingdom_food'];
$kingdom_wood = $king['kingdom_wood'];
$kingdom_iron = $king['kingdom_iron'];
$kingdom_gold = $king['kingdom_gold'];
$kingdom_stamina = $king['kingdom_stamina'];
$kingdom_turns = $king['kingdom_turns'];
$kingdom_unit_bonus = $king['kingdom_unit_bonus'];
$kingdom_status = $king['kingdom_status'];

$kingdom_net_penalty = ($kingdom_net * $penalty);

if($kingdom_net_penalty < 0 )
{
	$kingdom_net_penalty = 0;
}
// Check to see if this user is a admin staff member
if ($user_id == 2 || $admin_access == 1)
{
	$admin = 1;
}

// Check kingdom status to see if this account is frozen
if($kingdom_status == 1)
{
	// Lets make sure the main admin can't be blocked
	if ($user_id <> 2){
		trigger_error($user->lang['FROZEN'] . back_link());
	}
}

// Land cost penalty
$land_cost = floor($land_gold + $kingdom_net_penalty);

// Cut the price in half for sponsors
if($kingdom_sponsor > 0)
{
	$land_cost = floor($land_cost / 2);	
}

// Explore Command
if($cmd == $user->lang['EXPLORE'])
{
	$amount = ($_POST['land']) ? ereg_replace("[^0-9]", "", $_POST['land']): 0;
	$gold_total = ($amount * $land_cost);
	
	if($gold_total > $kingdom_gold)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_GOLD'];
	}
	
	if(!$error){
	
		$db->sql_query(
		"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
		kingdom_land = kingdom_land + $amount,
		kingdom_gold = kingdom_gold - $gold_total
		WHERE user_id = $user_id");
		$db->sql_return_on_error(false);
	
		
		// Build the report
		$report = sprintf($user->lang['SUCCESSFUL_LAND_EXPLORE'] , number_format($amount) , number_format($gold_total));
		
		// Log the report in the database
		report_transaction($user_id,$report,'land.php',$date);
		
		// Now it is time to redirect the user to see the report page so they can see the report
		redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));			
	}
}

// Build Command
if($cmd == $user->lang['BUILD'])
{

	$sql = "SELECT *
	FROM " . AOD_BUILDINGS_TABLE . "	
	ORDER BY id ASC";
	$result = $db->sql_query($sql);
				
	while ($row = $db->sql_fetchrow($result))
	{
	
		$bid = $row['id'];

		$building[$bid]['name'] = $row['building_name'];
		$building[$bid]['amount'] = ($_POST[$bid]) ? ereg_replace("[^0-9]", "", $_POST[$bid]): 0;
		
		$building[$bid]['gold'] = ($building[$bid]['amount'] * ($row['building_gold'] + $kingdom_net_penalty ));
		$building[$bid]['wood'] = ($building[$bid]['amount'] * $row['building_wood']);
		$building[$bid]['iron'] = ($building[$bid]['amount'] * $row['building_iron']);
	
		$land_total = ($land_total +  $building[$bid]['amount']);
		$gold_total = round($gold_total +  $building[$bid]['gold']);
		$wood_total = ($wood_total +  $building[$bid]['wood']);
		$iron_total = ($iron_total +  $building[$bid]['iron']);
						
	}
	$db->sql_freeresult($result);	
	
	if($gold_total > $kingdom_gold)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_GOLD'];
	}
	
	
	if($wood_total > $kingdom_wood)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_WOOD'];
	}
	
	
	if($iron_total > $kingdom_iron)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_IRON'];
	}
	

	if($land_total > $kingdom_land)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_LAND'];
	}	
	

	
	if($land_total < 1)
	{
		$error = TRUE;
	}

	
	if(!$error)
	{
		
		// Farms
		if($building[1]['amount'] > 0)
		{
			$sql_data[] = "kingdom_farms = kingdom_farms + ".$building[1]['amount'];
			if($building[1]['amount'] == 1){
				$building_msg[0] = $building[1]['amount'].' Farm';
			}else{
				$building_msg[0] = number_format($building[1]['amount']).' Farms';
			}
		}		

		// Timber Mills
		if($building[2]['amount'] > 0)
		{
			$sql_data[] = "kingdom_timber_mills = kingdom_timber_mills + ".$building[2]['amount'];
			if($building[2]['amount'] == 1){
				$building_msg[1] = $building[2]['amount'].' Timber Mill';
			}else{
				$building_msg[1] = number_format($building[2]['amount']).' Timber Mills';
			}
		}		

		// Iron Mines
		if($building[3]['amount'] > 0)
		{
			$sql_data[] = "kingdom_iron_mines = kingdom_iron_mines + ".$building[3]['amount'];
			if($building[3]['amount'] == 1){
				$building_msg[2] = $building[3]['amount'].' Iron Mine';
			}else{
				$building_msg[2] = number_format($building[3]['amount']).' Iron Mines';
			}
		}		

		// Gold Mines
		if($building[4]['amount'] > 0)
		{
			$sql_data[] = "kingdom_gold_mines = kingdom_gold_mines + ".$building[4]['amount'];
			if($building[4]['amount'] == 1){
				$building_msg[3] = $building[4]['amount'].' Gold Mine';
			}else{
				$building_msg[3] = number_format($building[4]['amount']).' Gold Mines';
			}
		}		

		// Towers
		if($building[5]['amount'] > 0)
		{
			$sql_data[] = "kingdom_towers = kingdom_towers + ".$building[5]['amount'];
			if($building[5]['amount'] == 1){
				$building_msg[4] = $building[5]['amount'].' Tower';
			}else{
				$building_msg[4] = number_format($building[5]['amount']).' Towers';
			}
		}		

		// Granaries
		if($building[6]['amount'] > 0)
		{
			$sql_data[] = "kingdom_granaries = kingdom_granaries + ".$building[6]['amount'];
			if($building[6]['amount'] == 1){
				$building_msg[5] = $building[6]['amount'].' Granary';
			}else{
				$building_msg[5] = number_format($building[6]['amount']).' Granaries';
			}
		}		

		// Woodsheds
		if($building[7]['amount'] > 0)
		{
			$sql_data[] = "kingdom_woodsheds = kingdom_woodsheds + ".$building[7]['amount'];
			if($building[7]['amount'] == 1){
				$building_msg[6] = $building[7]['amount'].' Woodshed';
			}else{
				$building_msg[6] = number_format($building[7]['amount']).' Woodsheds';
			}
		}		

		// Iron Pits
		if($building[8]['amount'] > 0)
		{
			$sql_data[] = "kingdom_ironpits = kingdom_ironpits + ".$building[8]['amount'];
			if($building[8]['amount'] == 1){
				$building_msg[7] = $building[8]['amount'].' Iron Pit';
			}else{
				$building_msg[7] = number_format($building[8]['amount']).' Iron Pits';
			}
		}		

		// Vaults
		if($building[9]['amount'] > 0)
		{
			$sql_data[] = "kingdom_vaults = kingdom_vaults + ".$building[9]['amount'];
			if($building[9]['amount'] == 1){
				$building_msg[8] = $building[9]['amount'].' Vault';
			}else{
				$building_msg[8] = number_format($building[9]['amount']).' Vaults';
			}
		}		
		
		// Build SQL data list
		$sql_data = build_sql_list($sql_data);
		$building_msg = build_list($building_msg);
		
		$db->sql_query(
		"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
		kingdom_land = kingdom_land - $land_total,
		kingdom_gold = kingdom_gold - $gold_total,
		kingdom_wood = kingdom_wood - $wood_total,
		kingdom_iron = kingdom_iron - $iron_total,
		$sql_data
		WHERE user_id = $user_id");
		$db->sql_return_on_error(false);
	
		
		// Build the report
		$report = sprintf($user->lang['SUCCESSFUL_BUILD_1'],number_format($land_total)).$building_msg.sprintf($user->lang['SUCCESSFUL_BUILD_2'] , number_format($gold_total) , number_format($wood_total) , number_format($iron_total));
		
		// Log the report in the database
		report_transaction($user_id,$report,'land.php',$date);
		
		// Now it is time to redirect the user to see the report page so they can see the report
		redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));		
	}
			
}

// Raze Command
if($cmd == $user->lang['RAZE'])
{
	$sql = "SELECT *
	FROM " . AOD_BUILDINGS_TABLE . "	
	ORDER BY id ASC";
	$result = $db->sql_query($sql);
				
	while ($row = $db->sql_fetchrow($result))
	{
	
		$bid = $row['id'];

		$building[$bid]['name'] = $row['building_name'];
		$building[$bid]['amount'] = ($_POST[$bid]) ? ereg_replace("[^0-9]", "", $_POST[$bid]): 0;
		
		$building[$bid]['gold'] = ($building[$bid]['amount'] * ($row['building_gold'] + $kingdom_net_penalty ));
		$building[$bid]['wood'] = ($building[$bid]['amount'] * $row['building_wood']);
		$building[$bid]['iron'] = ($building[$bid]['amount'] * $row['building_iron']);
	
		$land_total = ($land_total +  $building[$bid]['amount']);
		$gold_total = round($gold_total +  $building[$bid]['gold']);
		$wood_total = ($wood_total +  $building[$bid]['wood']);
		$iron_total = ($iron_total +  $building[$bid]['iron']);
						
	}
	$db->sql_freeresult($result);	
	
	$gold_total = floor($gold_total / 2);
	$wood_total = floor($wood_total / 2);
	$iron_total = floor($iron_total / 2);
	
	// Farms
	if($building[1]['amount'] > $kingdom_farms)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B1'];
		
	}else{
		
		$sql_data[] = "kingdom_farms = kingdom_farms - ".$building[1]['amount'];
		
		if($building[1]['amount'] == 1){
			$building_msg[0] = $building[1]['amount'].' Farm';
		}
		
		if($building[1]['amount'] > 1){
			$building_msg[0] = number_format($building[1]['amount']).' Farms';	
		}
	}		

	// Timber Mills
	if($building[2]['amount'] > $kingdom_timber_mills)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B2'];
		
	}else{
		
		$sql_data[] = "kingdom_timber_mills = kingdom_timber_mills - ".$building[2]['amount'];
		
		if($building[2]['amount'] == 1){
			$building_msg[1] = $building[2]['amount'].' Timber Mill';
		}
		
		if($building[2]['amount'] > 1){
			$building_msg[1] = number_format($building[2]['amount']).' Timber Mills';
		}
	}		

	// Iron Mines
	if($building[3]['amount'] > $kingdom_iron_mines)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B3'];
		
	}else{
		
		$sql_data[] = "kingdom_iron_mines = kingdom_iron_mines - ".$building[3]['amount'];
		
		if($building[3]['amount'] == 1){
			$building_msg[2] = $building[3]['amount'].' Iron Mine';
		}
		
		if($building[3]['amount'] > 1){
			$building_msg[2] = number_format($building[3]['amount']).' Iron Mines';
		}
	}		

	// Gold Mines
	if($building[4]['amount'] > $kingdom_gold_mines)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B4'];
		
	}else{
		
		$sql_data[] = "kingdom_gold_mines = kingdom_gold_mines - ".$building[4]['amount'];
		
		if($building[4]['amount'] == 1){
			$building_msg[3] = $building[4]['amount'].' Gold Mine';
		}
		
		if($building[4]['amount'] > 1){
			$building_msg[3] = number_format($building[4]['amount']).' Gold Mines';
		}
	}		

	// Towers
	if($building[5]['amount'] > $kingdom_towers)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B5'];
		
	}else{
		
		$sql_data[] = "kingdom_towers = kingdom_towers - ".$building[5]['amount'];
		
		if($building[5]['amount'] == 1){
			$building_msg[4] = $building[5]['amount'].' Tower';
		}
		
		if($building[5]['amount'] > 1){
			$building_msg[4] = number_format($building[5]['amount']).' Towers';
		}
	}		

	// Granaries
	if($building[6]['amount'] > $kingdom_granaries)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B6'];
		
	}else{
		
		$sql_data[] = "kingdom_granaries = kingdom_granaries - ".$building[6]['amount'];
		
		if($building[6]['amount'] == 1){
			$building_msg[5] = $building[6]['amount'].' Granary';
		}
		
		if($building[6]['amount'] > 1){
			$building_msg[5] = number_format($building[6]['amount']).' Granaries';
		}
	}		

	// Woodsheds
	if($building[7]['amount'] > $kingdom_woodsheds)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B7'];
		
	}else{
		
		$sql_data[] = "kingdom_woodsheds = kingdom_woodsheds - ".$building[7]['amount'];
		
		if($building[7]['amount'] == 1){
			$building_msg[6] = $building[7]['amount'].' Woodshed';
		}
		
		if($building[7]['amount'] > 1){
			$building_msg[6] = number_format($building[7]['amount']).' Woodsheds';
		}
	}		

	// Iron Pits
	if($building[8]['amount'] > $kingdom_ironpits)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B8'];
		
	}else{
		
		$sql_data[] = "kingdom_ironpits = kingdom_ironpits - ".$building[8]['amount'];
		
		if($building[8]['amount'] == 1){
			$building_msg[7] = $building[8]['amount'].' Iron Pit';
		}
		
		if($building[8]['amount'] > 1){
			$building_msg[7] = number_format($building[8]['amount']).' Iron Pits';
		}
	}		

	// Vaults
	if($building[9]['amount'] > $kingdom_vaults)
	{
		$error = TRUE;
		$error_msgs .= $user->lang['NOT_ENOUGH_B9'];
		
	}else{
		
		$sql_data[] = "kingdom_vaults = kingdom_vaults - ".$building[9]['amount'];
		
		if($building[9]['amount'] == 1){
			$building_msg[8] = $building[9]['amount'].' Vault';
		}
		
		if($building[9]['amount'] > 1){
			$building_msg[8] = number_format($building[9]['amount']).' Vaults';
		}
	}

	if(!$error)
	{
		// Build SQL data list
		$sql_data = build_sql_list($sql_data);
		$building_msg = build_list($building_msg);
		
		$db->sql_query(
		"UPDATE " . AOD_KINGDOMS_TABLE ." SET 
		kingdom_land = kingdom_land + $land_total,
		kingdom_gold = kingdom_gold + $gold_total,
		kingdom_wood = kingdom_wood + $wood_total,
		kingdom_iron = kingdom_iron + $iron_total,
		$sql_data
		WHERE user_id = $user_id");
		$db->sql_return_on_error(false);
	
		
		// Build the report
		$report = $user->lang['SUCCESSFUL_RAZED_1'].$building_msg.sprintf($user->lang['SUCCESSFUL_RAZED_2'] , number_format($gold_total) , number_format($wood_total) , number_format($iron_total));
		
		// Log the report in the database
		report_transaction($user_id,$report,'land.php',$date);
		
		// Now it is time to redirect the user to see the report page so they can see the report
		redirect(append_sid("{$phpbb_root_path}/games/aod/report.$phpEx"));		
	}		
}

$buildingcount = fetch_kingdom_building_counts($user_id);

// Build unit list	
$sql = "SELECT *
FROM " . AOD_BUILDINGS_TABLE . "	
ORDER BY id ASC";
$result = $db->sql_query($sql);
			
while ($row = $db->sql_fetchrow($result))
{

	$bid = $row['id'];
	$building_name = $row['building_name'];
	$building_net = $row['building_net'];
	$building_def = $row['building_def'];
	$building_gold = $row['building_gold'];
	$building_wood = $row['building_wood'];
	$building_iron = $row['building_iron'];
	
	$gold_max = floor($kingdom_gold / ($building_gold + $kingdom_net_penalty));
	$wood_max = floor($kingdom_wood / $building_wood);
	$iron_max = floor($kingdom_iron / $building_iron);

	$building_max = NULL;
	$building_max = min($kingdom_land,$gold_max,$wood_max,$iron_max);

	$template->assign_block_vars('buildingrow', array(
		'BNAME'		=> $building_name,
		'BNET'		=> number_format($building_net),
		'BDEF'		=> number_format($building_def),
		'BGOLD'		=> number_format($building_gold + $kingdom_net_penalty),
		'BWOOD'		=> number_format($building_wood),
		'BIRON'		=> number_format($building_iron),
		'BOWN'		=> number_format($buildingcount[$bid]),
		'BMAX'		=> number_format($building_max),
		'BAMOUNT'	=> '<input type="text" class="game_post" maxlength="8" name="'.$bid.'" style="width:94%;">',		
		)
	);	
}	
$db->sql_freeresult($result);

// Display an error messages to the user
if($error_msgs){
	$error_msgs = '<span class="errormsg"><b>'.$error_msgs.'</b></span>';
}

$template->assign_vars(array(
	'EXPLORATION'			=> sprintF($user->lang['EXPLORATION_EXPLAIN'], number_format($land_cost)),
	'LAND_MAX'				=> number_format(floor($kingdom_gold / $land_cost)),
	'EXPLORED'				=> sprintF($user->lang['LAND_EXPLORED'],number_format($kingdom_land)),
	)
);

include($phpbb_root_path . 'games/aod/includes/page_header.' . $phpEx);


// Output page
page_header($game_name.' - '.$user->lang['LAND']);

$template->set_filenames(array(
	'body' => $templates_path.'land_body.html')
);

page_footer();
?>